<?php

namespace App\Config;

use Workerman\MySQL\Connection;
class connectMysql extends Connection{

    /**
     * 静态成品变量 保存全局实例
     */
    private static $_instance = array();

    /**
     * @param $config
     * @return Connection
     */
    public static function getInstance($config)
    {
        $key = md5(implode(":", $config));
        //判断实例是否存在并且连接有效
        if (!isset(self::$_instance[$key])) {
            $host = isset($config["host"]) ? $config["host"] : "127.0.0.1";
            $port = isset($config["port"]) ? $config["port"] : 3306;
            $password = isset($config["password"]) ? $config["password"] : "";
            $db_name = isset($config["db_name"]) ? $config["db_name"] : "dbprint";
            $username = isset($config["username"]) ? $config["username"] : "root";
            self::$_instance[$key] = new self($host, $port, $username, $password, $db_name);
        }
        return self::$_instance[$key];
    }

}